/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// @ts-ignore
import testNapi from '@ohos.idcard'
import util from '@ohos.util';
const TAG: string = '[IdCard]'
import prompt from '@ohos.promptAction'

export class IdCard {

  private dataimageStr: string = ''

  public imageChangeFunction: (image: string) => void = () => {}

  onReadCard(ret) {
    const dataimage = ret.photo
    let bufferArr = new Uint8Array(dataimage)
    let helper = new util.Base64Helper()
    this.dataimageStr = helper.encodeToStringSync(bufferArr)
    // this.imageChangeFunction(this.dataimageStr)
    // that.base64Str = dataimageStr
    const isstring = typeof dataimage === 'string'
    console.log('daihai onReadCard is string： ' +  isstring)
    if(ret.status == 0) { // status = 1 为读到身份证信息； status = 1 为身份证离开读卡器
      // 收到身份证离开读卡器的事件通知，根据实际业务需要决定是否需要清空之前已读取的身份证信息。
      // 如果身份证卡片离开读卡器时，需要继续保留UI界面上已读取到的身份信息，以下代码可以注释掉。

      return true;
    }

    if (ret.baseInfo.length > 2) {
      // 收到身份证信息，填充页面
      console.info(`iDR Read Card 1`);
      let subIndex = ret.baseInfo.indexOf(":")
      let baseInfo: string = ret.baseInfo.substring(subIndex + 1, ret.baseInfo.length - 1);
      console.info(`iDR Read Card 2 baseInfo=${baseInfo}`);
      prompt.showToast({
        message: `${baseInfo}`,
        duration: 2000
      });
    }

    // 身份证照片数据处理
    console.info(`iDR Read image ret.photo: ` + ret.photo);

    //    const data: ArrayBuffer = ret.photo
    //    var utf8 = new Uint8Array(data);
    //    console.log('iDR decodedstring START')
    //    var decodedString = String.fromCharCode.apply(null, utf8)
    //    console.log('iDR decodedstring:' + decodedString)




    //    if (imageSource) {
    //      console.log(`iDR Read imageSource if`);
    //
    //    } else {
    //      console.log(`iDR Read imageSource else`);
    //    }

    // 身份证照片数据处理
    //    if (ret.photo instanceof ArrayBuffer) {
    //      let dataView = new DataView(ret.photo)
    //      console.info(`Read Card ret = length = ${dataView.byteLength}`)
    //      let str = ""
    //      for (let i = 0;i < dataView.byteLength; ++i) {
    //        let c = String.fromCharCode(dataView.getUint8(i))
    //        if (c !== "\n") {
    //          str += c
    //        }
    //      }
    //      thisVar.photo += str;
    //    }
    console.info(`iDR Read Card end`);
    return true;
  }

  readInfo() {
    testNapi.StartReadCard(this.onReadCard);
  }
}